import 'package:dartkit/dartkit.dart';
import 'package:test/test.dart';

const String _phone = "12345678901";

void main() {
  prepareAndCleanPostgres("login_handler_test_db");

  group("发送验证码", () {
    final handler = pipelineGeneral.addHandler(sendSms);

    test("正常回复", () async {
      // 创建一个request，并将测试用的手机号放到body的Map中
      final request = testRequest(body: {"phone": _phone});
      // 用handler处理请求，在测试环境下，可以得到一个4位数的验证码在body中
      final response = await handler(request);
      expect(response.statusCode, 200);
      final String body = await response.readAsString();
      expect(body, hasLength(4));
    });

    test("错误的格式", () async {
      // 创建一个错误的手机号格式，结果应该收到400错误
      final request = testRequest(body: {"phone": "123"});
      // 用handler处理请求，在测试环境下，可以得到一个4位数的验证码在body中
      final response = await handler(request);
      expect(response.statusCode, 400);
    });

    test("没有手机号", () async {
      // 创建一个空请求，不带手机号，结果应该收到400错误
      final request = testRequest();
      // 用handler处理请求，在测试环境下，可以得到一个4位数的验证码在body中
      final response = await handler(request);
      expect(response.statusCode, 400);
    });

    test("重复发送", () async {
      // 连续发送2次请求
      final request1 = testRequest(body: {"phone": _phone});
      // 第一次成功
      final response1 = await handler(request1);
      expect(response1.statusCode, 200);
      // 第二次由于冷却应该报错
      // 同一个request不能读2次，因此这里需要重新构建一个request
      final request2 = testRequest(body: {"phone": _phone});
      final response2 = await handler(request2);
      expect(response2.statusCode, 400);
    });
  });

  group("核销验证码", () {
    setUpAll(() {
      // 需要先设置token的secret
      argSecret.value = "secret";
    });

    final handler = pipelineGeneral.addHandler(redeemSms);

    test("缺少内容", () async {
      // 如果提交的请求中少了手机或者验证码，都会返回400错误
      final missingPhone = testRequest(body: {"code": "1234"});
      final response1 = await handler(missingPhone);
      expect(response1.statusCode, 400);
      final missingCode = testRequest(body: {"phone": _phone});
      final response2 = await handler(missingCode);
      expect(response2.statusCode, 400);
    });

    test("没发送过验证码", () async {
      // 如果直接随便填一个验证码发送给服务器，则会返回400错误
      final request = testRequest(body: {"phone": _phone, "code": "1234"});
      final response = await handler(request);
      expect(response.statusCode, 400);
    });

    test("顺利提交", () async {
      // 首先创建一个验证码，并获取验证码
      final sendSmsHandler = pipelineGeneral.addHandler(sendSms);
      final sendSmsRequest = testRequest(body: {"phone": _phone});
      final response = await sendSmsHandler(sendSmsRequest);
      final String code = await response.readAsString();
      // 这时，提交一个随便的验证码，能得到400错误
      final request2 = testRequest(body: {"phone": _phone, "code": "1234"});
      final response2 = await handler(request2);
      expect(response2.statusCode, 400);
      // 如果将正确的验证码填上，就发现是200，并且得到了一个token
      final request3 = testRequest(body: {"phone": _phone, "code": code});
      final response3 = await handler(request3);
      expect(response3.statusCode, 200);
      final token = await response3.readAsString();
      // 解码token后，可以得到用户id为2（已经有一个用户存在了）
      final subject = TokenKit.redeem(token);
      expect(subject, equals("2"));
    });
  });
}
